package com.guoxin.concurrentprogramming.chapter4.producerconsumer;


import java.util.concurrent.*;

public class Main {

    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<PCData> queue = new LinkedBlockingQueue<>();
        //建立生产者
        Producer p1 = new Producer(queue);
        Producer p2 = new Producer(queue);
        Producer p3 = new Producer(queue);

        //建立消费者
        Consumer c1 = new Consumer(queue);
        Consumer c2 = new Consumer(queue);
        Consumer c3 = new Consumer(queue);

        //创建线程池
        /*ExecutorService service = new ThreadPoolExecutor(10, 10, 0L,
                TimeUnit.MILLISECONDS, new SynchronousQueue<>());*/
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(p1);
        service.execute(p2);
        service.execute(p3);
        service.execute(c1);
        service.execute(c2);
        service.execute(c3);

        TimeUnit.SECONDS.sleep(10);

        p1.stop();
        p2.stop();
        p3.stop();
        TimeUnit.SECONDS.sleep(3);

        service.shutdown();
    }
}
